from flask import Flask, request
from flask_cors import CORS
import json

app = Flask(__name__)

def month_traversal(date):
    month = date[-2:]
    year = date[:4]
    if month < 12:
        month += 1
    else:
        month = 1
        year += 1
    return "%s-%s" % (year, month)

@app.route('/')
def get_merged_json():
    since = request.args.get('since', default="2024-01")
    until = request.args.get('until', default=since)
    if since > until:
        return "Invalid params"
    currdate = since
    ### merge 合并时间段内列表
    list1 = []
    with open('./stat/%s/merged.json' % str(currdate)) as f:
        list1 = json.load(f)
    while currdate <= until:
        list2 = []
        with open('./stat/%s/merged.json' % str(currdate))  as f:
            list2 = json.load(f)
        data_list = []
        data_dict = {}
        for one in list1:
            name = one['name']
            additions = one['additions']
            deletions = one['deletions']
            comment = one['comment']
            commented = one['commented']
            pr_count = one['pr_count']
            data_dict[name] = {'additions': additions, 'deletions': deletions, 'comment': comment, 'commented': commented, 'pr_count': pr_count}
        for one in list2:
            name = one['name']
            additions = one['additions']
            deletions = one['deletions']
            comment = one['comment']
            commented = one['commented']
            pr_count = one['pr_count']
            if name in data_dict:
                data_dict[name]['additions'] += additions
                data_dict[name]['deletions'] += deletions
                data_dict[name]['comment'] += comment
                data_dict[name]['commented'] += commented
                data_dict[name]['pr_count'] += pr_count
            else:
                data_dict[name] = {'additions': additions, 'deletions': deletions, 'comment': comment, 'commented': commented, 'pr_count': pr_count}
        data_list = [{'name': k, 'additions': v['additions'], 'deletions': v['deletions'], 'comment': v['comment'], 'commented': v['commented'], 'pr_count': v['pr_count']} for k,v in data_dict.items()]
        currdate = month_traversal(currdate)
    ### TopThree 排名前三

    data_list.sort(key = lambda x: x['pr_count'], reverse=True)
    top_pr = []
    for j in range(3):
        if j < len(data_list):
            top_pr.append(data_list[j])
        else:
            break
    data_list.sort(key = lambda x: x['comment'], reverse=True)
    top_comment = []
    for j in range(3):
        if j < len(data_list):
            top_comment.append(data_list[j])
        else:
            break
    data_list.sort(key = lambda x: x['additions'], reverse=True)
    top_additons = []
    for j in range(3):
        if j < len(data_list):
            top_additons.append(data_list[j])
        else:
            break
    return {data_list, top_additons, top_pr, top_comment}
    # return data_list

if __name__ == '__main__':
    CORS(app, resources=r'/*')
    app.run()